package com.im.service;

import java.io.DataInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DecimalFormat;

import org.jivesoftware.smackx.filetransfer.FileTransferRequest;
import org.jivesoftware.smackx.filetransfer.IncomingFileTransfer;

import com.core.sdk.core.LogUtil;
import com.im.event.FileType;
import com.im.util.PathUtil;
import com.im.util.XmppUtil;

public abstract class FileReceivedThread extends Thread implements FileReceivedListener {
	private final String tag = "ReceivedFileThead";
	IncomingFileTransfer transfer = null;
	private final long fileSize;
	String fileName;
	FileType fileType;
	private String fromUser;
	final String description ;

	public FileReceivedThread(IncomingFileTransfer transfer, FileTransferRequest request) {
		super();
		this.transfer = transfer;
		this.fileSize = request.getFileSize();
		this.fileName = request.getFileName();
		String fn = this.fileName.trim().toLowerCase();
		String suffix = fn.substring(fn.lastIndexOf(".")+1);
		if (FileTypeUtil.isImage(suffix)) {
			this.fileType = FileType.img;
		} else if (FileTypeUtil.isAudio(suffix)) {
			this.fileType = FileType.audio;
		} else {
			this.fileType = FileType.other;
		}
		this.fromUser = XmppUtil.getUserNameByUserJID(request.getRequestor());
		this.description = request.getDescription();
		LogUtil.d(tag, "文件名:" + fileName + " 大小:" + fileSize + "字节,发送者:" + fromUser);
	}

	String savePath = null;
	@Override
	public void run() {

		FileOutputStream fos = null;
		DataInputStream dis = null;
		try {
			savePath = PathUtil.getCacheDir() + fileName;
			fos = new FileOutputStream(savePath);
			dis = new DataInputStream(transfer.recieveFile());
			byte[] buffer = new byte[1024];
			int rec = -1;
			double progress = 0;
			DecimalFormat df = new DecimalFormat("#.##");
			long total = 0;
			String progressText = null;
			while ((rec = dis.read(buffer, 0, buffer.length)) != -1) {
				total += rec;
				progress = total * 100d / fileSize;
				fos.write(buffer, 0, rec);
				fos.flush();
				progressText = df.format(progress);
				onReceivedProgress(progressText);
			}

			fos.close();
			fos = null;
			dis.close();
			dis = null;

			if (total == fileSize) {
				onReceivedSuccess();
			} else {
				throw new RuntimeException("total=" + total + " != " + fileSize);
			}
		} catch (Exception e) {
			onReceivedException(e);
		} finally {
			if (fos != null) {
				try {
					fos.close();
				} catch (Exception e2) {
				}
				fos = null;
			}
			if (dis != null) {
				try {
					dis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
				dis = null;
			}
		}

	}

	public String getFromUser() {
		return fromUser;
	}

	public FileType getFileType() {
		return fileType;
	}

	public String getFileName() {
		return fileName;
	}

	public String getSavePath() {
		return savePath;
	}

	public String getDescription() {
		return description;
	}
	

}
